package io.jee.runshan.handler;

import io.jee.runshan.domain.WeixinMenu;
import io.jee.runshan.domain.WeixinMenuContent;
import io.jee.runshan.domain.remote.weixin.Article;
import io.jee.runshan.domain.remote.weixin.InputMessage;
import io.jee.runshan.domain.remote.weixin.OutputImageTextMessage;
import io.jee.runshan.domain.remote.weixin.OutputTextMessage;
import io.jee.runshan.service.IWeixinMenuContentService;
import io.jee.runshan.service.IWeixinMenuService;
import io.jee.runshan.utils.SpringHolder;
import io.jee.runshan.utils.WeixinConstant;

import java.util.ArrayList;
import java.util.List;

/**
 * 菜单事件请求处理
 * 
 * @author qinling.zhu
 *
 */
public class WeixinReceiverEventHandler {

	public String excute(InputMessage inputMsg) throws Exception {
		String result = "";
		String eventKey = inputMsg.getEventKey();
		switch (eventKey) {
//		case "ask": // 问询服务
//			WeixinReceiverAskHandler askHandler = new WeixinReceiverAskHandler();
//			result = askHandler.excute(inputMsg);
//			break;
//		case "floor": // 楼层简介
//			WeixinReceiverFloorHandler floorHandler = new WeixinReceiverFloorHandler();
//			result = floorHandler.excute(inputMsg);
//			break;
//		case "activities": // 最新活动
//			WeixinReceiverActivitiesHandler activitiesHandler = new WeixinReceiverActivitiesHandler();
//			result = activitiesHandler.excute(inputMsg);
//			break;
//		case "mall": // 商场简介
//			WeixinReceiverMallHandler mallHandler = new WeixinReceiverMallHandler();
//			result = mallHandler.excute(inputMsg);
//			break;
//		case "compay": // 公司简介
//			WeixinReceiverCompayHandler compayHandler = new WeixinReceiverCompayHandler();
//			result = compayHandler.excute(inputMsg);
//			break;
		case "internetFree": // 免费上网
			WeixinReceiverFreeinternateHandler freeInternate = new WeixinReceiverFreeinternateHandler();
			result = freeInternate.excute(inputMsg);
			break;
		case "subscribe":
			OutputTextMessage subscribeOutputMsg = new OutputTextMessage();
			subscribeOutputMsg.setContent("欢迎关注润山东方百货，马上开启您的购物旅程吧！");
			result = WeixinConstant.genMessage(subscribeOutputMsg, inputMsg);
			break;
		default:
			IWeixinMenuService menuService = SpringHolder.getBean(IWeixinMenuService.class);
			IWeixinMenuContentService contentService = SpringHolder.getBean(IWeixinMenuContentService.class);
			
			WeixinMenu menu = menuService.unique("menuKey", eventKey);
			if (menu != null) {
				List<WeixinMenuContent> contents = contentService.list("menu.id", menu.getId(), "DESC", "orderIndex");
				if (contents == null || contents.size() == 0) {
					break;
				}
				switch (menu.getReType()) {
				case 1: // 纯文字消息
					contentService.unique("menu.id", menu.getId());
					OutputTextMessage textResponse = new OutputTextMessage();
					textResponse.setContent(contents.get(0).getContent());
					result = WeixinConstant.genMessage(textResponse, inputMsg);
					break;
				case 2: // 单图文消息
				case 3: // 多图文消息
					List<Article> list = new ArrayList<Article>();
					OutputImageTextMessage imageResponse = new OutputImageTextMessage();
					Article article = null;
					for (WeixinMenuContent content : contents) {
						article = new Article();
						article.setDescription(content.getIntroducte());
						article.setTitle(content.getContent());
						article.setPicUrl(WeixinConstant.getRootUrl() + content.getThumbnailUrl());
						article.setUrl(WeixinConstant.getRootUrl() + "/web/weixin/menu/content/detail?id=" + content.getId());
						list.add(article);
					}
					if (list.size() > 0) {
						imageResponse.setArticleCount(list.size());
						imageResponse.setArticles(list);
						result = WeixinConstant.genMessage(imageResponse, inputMsg);
					}
				}
			}
		}
		return result;
	}
}
